package cn.tedu.ivos.vehicle.service.Impl;

import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;

    // 查询车辆
    @Override
    public List<VehicleVO> selectAll(VehicleQuery vehicleQuery) {
        log.debug("开始处理查询车辆信息业务:vehicleQuery={}",vehicleQuery);
        List<VehicleVO> list = vehicleMapper.selectAll(vehicleQuery);
        return list;
    }

    // 保存车辆
    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("开始处理保存车辆业务:vehicleSaveParam={}",vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        if (vehicle.getId()==null){
            vehicle.setStatus("1");
            vehicle.setCreateTime(new Date());
            vehicle.setGeofenceBindStatus("0");
            vehicleMapper.saveVehicle(vehicle);
        }else {
            //TODO 修改车辆信息
            vehicle.setUpdateTime(new Date());
            vehicleMapper.updateVehicle(vehicle);
        }
    }

    // 删除车辆
    @Override
    public void deleteVehicle(Long vehicleId) {
        log.debug("开始处理删除车辆业务:vehicleId={}",vehicleId);
        vehicleMapper.deleteVehicle(vehicleId);
    }

    @Override
    public void unbindGeofence(Long vehicleId) {
        log.debug("开始处理解除围栏绑定业务:vehicleId={}",vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(null);
        vehicleMapper.unbindGeofence(vehicle);
    }

    @Override
    public void bindGeofence(Long geoId, Long vehicleId) {
        log.debug("开始处理绑定围栏业务:geoId={},vehicleId={}",geoId,vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(geoId);
        vehicleMapper.updateVehicle(vehicle);
    }


}
